{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测结果[2]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "x=[[1],[2],[3],[4],[5]]\n",
    "y=[1,2,2,3,3]\n",
    "knn= KNeighborsClassifier(n_neighbors=3)\n",
    "knn.fit(x,y)\n",
    "print(\"预测结果{0}\".format(knn.predict([[2.3]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****************************KNN***********************\n",
      "precision:97.92%\n",
      "recall:97.78%\n",
      "accuracy:97.78%\n",
      "鸢尾花的分类是:[0]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "x_fit = iris.data\n",
    "y_fit = iris.target\n",
    "\n",
    "from sklearn.cross_validation import train_test_split\n",
    "x_train ,x_test ,y_train,y_test = train_test_split (x_fit,y_fit,test_size = 0.3)\n",
    "knn = KNeighborsClassifier(n_neighbors=3)\n",
    "knn.fit(x_train,y_train)\n",
    "pe = knn.predict(x_test)\n",
    "\n",
    "from sklearn import metrics\n",
    "print(\"*****************************KNN***********************\")\n",
    "print(\"precision:%.2f%%\"%(metrics.precision_score(y_test,pe,average='macro')*100))\n",
    "print(\"recall:%.2f%%\"%(metrics.recall_score(y_test,pe,average='macro')*100))\n",
    "print(\"accuracy:%.2f%%\"%(metrics.accuracy_score(y_test,pe)*100))\n",
    "\n",
    "print(\"鸢尾花的分类是:{0}\".format(knn.predict([[1.1,1.2,2.0,1.0]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****************************KNN***********************\n",
      "precision:97.92%\n",
      "recall:97.78%\n",
      "accuracy:97.78%\n",
      "鸢尾花的分类是:[0]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "x_fit = iris.data\n",
    "y_fit = iris.target\n",
    "\n",
    "from sklearn.cross_validation import train_test_split\n",
    "x_train ,x_test ,y_train,y_test = train_test_split (x_fit,y_fit,test_size = 0.3)\n",
    "knn = KNeighborsClassifier(n_neighbors=3)\n",
    "knn.fit(x_train,y_train)\n",
    "pe = knn.predict(x_test)\n",
    "\n",
    "from sklearn import metrics\n",
    "print(\"*****************************KNN***********************\")\n",
    "print(\"precision:%.2f%%\"%(metrics.precision_score(y_test,pe,average='macro')*100))\n",
    "print(\"recall:%.2f%%\"%(metrics.recall_score(y_test,pe,average='macro')*100))\n",
    "print(\"accuracy:%.2f%%\"%(metrics.accuracy_score(y_test,pe)*100))\n",
    "\n",
    "print(\"鸢尾花的分类是:{0}\".format(knn.predict([[1.1,1.2,2.0,1.0]])))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
